package com.sduept.nwld.dataserver.controller.daily;

import com.sduept.cim.model.core.Substation;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.nwld.dataserver.manager.protection.ProtectDefectManager;
import com.sduept.nwld.dataserver.model.daily.ProtectDefect;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import javax.faces.context.FacesContext;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Named
@ViewScoped
public class ProtectDefectController extends DailyReportAbstractController {

	private static final long serialVersionUID = 4439107821691600272L;

	@Autowired
	private ProtectDefectManager pd;
	@Autowired
	private CimManager cm;
	private ProtectDefect selectProtectDefect = null;
	private ProtectDefect created = null;
	private ProtectDefect selected = null;
	private List<ProtectDefect> defect = new ArrayList<>();

	@PostConstruct
	private void init() {
		defect = pd.getProtectDefect(queryDate1, queryDate2);
	}

	public void defectRowSelect(SelectEvent event) {
		selectProtectDefect = (ProtectDefect) event.getObject();
	}

	public void deleteProtectDefect() {
		if (selectProtectDefect != null) {
			pd.delProtectDefect(selectProtectDefect);
			defect.remove(selectProtectDefect);
			selectProtectDefect = null;
		} else {
			addErrorMessage("请选择一条数据进行删除");
		}
		addSuccessMessage("删除成功！");
	}

	public void addSuccessMessage(String msg) {
		String successMsg = "成功!";
		String titleMsg = "提示";
		FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, successMsg, msg);
		FacesContext.getCurrentInstance().addMessage(titleMsg, facesMsg);
	}

	/**
	 * 新建按钮触发事件
	 */
	public void createProtectDefect() {
		created = new ProtectDefect();
		created.setProtectInTime(new Date());
		created.setProtectQuitTime(new Date());
	}

	/**
	 * opendialog保存按钮触发事件
	 */
	public void saveNew() {
		if (created != null) {
			if (created.getSubstationName() != null) {
				List<Substation> s = cm.getStationByName(created.getSubstationName());
				if (s != null && s.size() > 0) {
					created.setSubstationId(s.get(0).getId());
				}
			}
			created = pd.creatProtectDefect(created);
			defect.add(0, created);
		}
		created = null;
		addSuccessMessage("保存成功");
	}

	public void resetQuery() {
		queryDate1 = null;
		queryDate2 = null;
		resetqueryDate();
		queryByTime();
	}

	public void queryByTime() {
		if (queryDate1 == null) {
			queryDate1 = new Date(0);
		}
		if (queryDate2 == null) {
			queryDate2 = new Date();
		}
		defect = pd.getProtectDefect(queryDate1, queryDate2);
		refreshQueryParamter();
		selected = null;
		created = null;
		selectProtectDefect = null;
	}

	public void update() {
		if (selectProtectDefect != null) {
			if (selectProtectDefect.getSubstationName() != null) {
				List<Substation> s = cm.getStationByName(selectProtectDefect.getSubstationName());
				if (s != null && s.size() > 0) {
					selectProtectDefect.setSubstationId(s.get(0).getId());
				}
			}
			pd.editUpdate(selectProtectDefect);
			addSuccessMessage("保存成功");
		}
	}

	public String replaceWithBr(String str) {
		str = str.replaceAll("(\r\n|\r|\n|\n\r)", "<br/>");
		return str;
	}

	public List<ProtectDefect> getDefect() {
		return defect;
	}

	public void setDefect(List<ProtectDefect> defect) {
		this.defect = defect;
	}

	public ProtectDefect getSelectProtectDefect() {
		return selectProtectDefect;
	}

	public void setSelectProtectDefect(ProtectDefect selectProtectDefect) {
		this.selectProtectDefect = selectProtectDefect;
	}

	public ProtectDefect getCreated() {
		return created;
	}

	public void setCreated(ProtectDefect created) {
		this.created = created;
	}

	public ProtectDefect getSelected() {
		return selected;
	}

	public void setSelected(ProtectDefect selected) {
		this.selected = selected;
	}

}
